from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager
import uvicorn

from api.processing.api import processing_api
from store.store_handler import StoreHandler


# 初始化操作的函数
def initialize_data(app: FastAPI):
    store_handler = StoreHandler()
    store_handler.init_config()
    app.store_handler = store_handler
    print("服务启动初始化完成")


# 生命周期实现
@asynccontextmanager
async def lifespan(app: FastAPI):
    initialize_data(app)
    yield


app = FastAPI(lifespan=lifespan)

# 允许所有来源的跨域请求
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"]
)

# 将分组添加到应用程序
app.include_router(processing_api, prefix="/api/docs", tags=["文档解析API"])

if __name__ == "__main__":
    uvicorn.run(app="main:app", host="0.0.0.0", port=9100, workers=10, reload=True, log_level="info")
